part of shop;

class LoginPage extends StatefulWidget {
  const LoginPage({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() => LoginPageState();
}

class LoginPageState extends State {
  final User _user=User();
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final ValueNotifier<int> _notifier = ValueNotifier(0);
  String log = '';
  String? phone;
  String? passwd;
  bool agree = false;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        automaticallyImplyLeading: false,
        backgroundColor: Colors.white,
        elevation: 0,
        leading: IconButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          icon: const Icon(
            Icons.arrow_back_ios_rounded,
            color: Colors.black45,
          ),
        ),
      ),
      body: Padding(
        padding: const EdgeInsets.fromLTRB(30, 5, 30, 20),
        child: Column(children: <Widget>[
          const FlutterLogo(
            size: 80,
          ),
          const Divider(
            height: 40,
          ),
          Form(
            key: _formKey,
            child: Column(children: <Widget>[
              TextFormField(
                decoration: const InputDecoration(
                  hintText: '手机号码',
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(15))),
                ),
                keyboardType: TextInputType.phone,
                textInputAction: TextInputAction.next,
                maxLength: 11,
                validator: (value) {
                  RegExp reg = RegExp(r'^\d{11}$');
                  if (value == null || !reg.hasMatch(value)) {
                    return '请输入11位手机号码';
                  }
                  return null;
                },
                onSaved: (value) {
                  phone = value;
                },
              ),
              const SizedBox(
                height: 10,
              ),
              TextFormField(
                textInputAction: TextInputAction.done,
                maxLength: 64,
                obscureText: true,
                decoration: const InputDecoration(
                  hintText: '密码',
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.all(Radius.circular(15))),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '密码不能为空';
                  }
                  return null;
                },
                onSaved: (value) {
                  passwd = value;
                },
              ),
              ButtonBar(alignment: MainAxisAlignment.end, children: [
                Text(
                  log,
                  style: const TextStyle(fontSize: 12, color: Colors.redAccent),
                ),
                TextButton(
                  // style: OutlinedButton.styleFrom(
                  //   padding: const EdgeInsets.fromLTRB(10, 7, 10, 7),
                  //   shape: RoundedRectangleBorder(
                  //     borderRadius: BorderRadius.circular(10.5),
                  //   ),
                  //   side: BorderSide(width: 2, color: Colors.blue[100]!),
                  // ),
                  onPressed: () {
                    if (_formKey.currentState!.validate()) {
                      if (agree == false) {
                        setState(() {
                          log = "请先阅读用户协议";
                        });
                        return;
                      }
                      _notifier.value = 1;
                      _formKey.currentState!.save();
                      _user
                          .login(tel: phone!, password: passwd!)
                          .then((value) {
                        if (value == null) {
                          Navigator.of(context).pop(0);
                        } else {
                          if (mounted) {
                            setState(() {
                              log = value;
                              _notifier.value = 0;
                            });
                          }
                        }
                      });
                    }
                  },
                  child: ValueListenableBuilder(
                    valueListenable: _notifier,
                    builder: (BuildContext context, int value, Widget? child) {
                      return [
                        const Text(
                          " 登录 ",
                          style: TextStyle(
                              fontSize: 26, fontWeight: FontWeight.bold),
                        ),
                        const CircularProgressIndicator()
                      ][value];
                    },
                  ),
                ),
              ]),
            ]),
          ),
        ]),
      ),
      bottomNavigationBar: SizedBox(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const Text("我已阅读并同意",
              style: TextStyle(color: Colors.black87, fontSize: 13)),
          TextButton(
            child: const Text(
              "用户协议",
              style: TextStyle(color: Colors.blue, fontSize: 14),
            ),
            onPressed: () {
              Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => const Agreement()));
            },
          ),
          Checkbox(
            value: agree,
            onChanged: (bool? value) {
              if (value != null) {
                agree = value;
                setState(() {});
              }
            },
          )
        ],
      )),
    );
  }
}

class Agreement extends StatelessWidget {
  const Agreement({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: const Text("用户协议"),
          backgroundColor: Colors.white,
        ),
        body: Padding(
          padding: const EdgeInsets.all(10),
          child: Text(
            agreement,
            maxLines: 20,
            overflow: TextOverflow.fade,
          ),
        ),
      );
  final String agreement = '''
抵制不良游戏，拒绝盗版游戏。
注意自我保护，谨防受骗上当。
适度游戏益脑，沉迷游戏伤身。
合理安排时间，享受健康生活。
  ''';
}
